﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;

namespace Test
{
    public class TestMD5
    {
        public static String MD5(String inStr)
        {
            if (String.IsNullOrEmpty(inStr)) return "";
            String outputStr = String.Empty;
            Byte[] dataToHash = (new System.Text.ASCIIEncoding()).GetBytes(inStr);
            Byte[] hashvalue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(dataToHash);
            Int32 i = 0;
            //选择32位字符的加密结果
            for (i = 0; i <= 15; i++)
            {
                //加上 PadLeft(2, "0"c) 是为了确保返回结果是两个字符，保证加密结构的长度是32位
                outputStr += Conversion.Hex(hashvalue[i]).ToString().PadLeft(2, '0');
            }
            return outputStr;
        }
    }
}
